<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <title>FindGTest &mdash; CMake 4.1.1 Documentation</title>

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a2c47e09" />
    <link rel="stylesheet" type="text/css" href="../_static/cmake.css?v=4d06bd55" />
    
    <script src="../_static/documentation_options.js?v=e6a937a4"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../_static/cmake-favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="FindGTK" href="FindGTK.html" />
    <link rel="prev" title="FindGSL" href="FindGSL.html" />
 

  </head><body>
    <input id="sidebar-check" type="checkbox" />
    <label id="sidebar-overlay" for="sidebar-check"></label>



    <div class="related relbar1" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="FindGTK.html" title="FindGTK"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="FindGSL.html" title="FindGSL"
             accesskey="P">previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" accesskey="U">cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">FindGTest</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="findgtest">
<span id="module:FindGTest"></span><h1>FindGTest<a class="headerlink" href="#findgtest" title="Link to this heading">¶</a></h1>
<p>Finds GoogleTest, the Google C++ testing and mocking framework:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">find_package(</span><span class="nb">GTest</span><span class="w"> </span><span class="p">[...]</span><span class="nf">)</span>
</pre></div>
</div>
<p>The GoogleTest framework also includes GoogleMock, a library for writing
and using C++ mock classes.  On some systems, GoogleMock may be distributed
as a separate package.</p>
<p>When both debug and release (optimized) variants of the GoogleTest and
GoogleMock libraries are available, this module selects the appropriate
variants based on the current <a class="reference internal" href="../manual/cmake-buildsystem.7.html#build-configurations"><span class="std std-ref">Build Configuration</span></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.20: </span>If GoogleTest is built and installed using its CMake-based build system, it
provides a <a class="reference internal" href="../manual/cmake-packages.7.html#config-file-packages"><span class="std std-ref">package configuration file</span></a>
(<code class="docutils literal notranslate"><span class="pre">GTestConfig.cmake</span></code>) that can be used with <span class="target" id="index-0-command:find_package"></span><a class="reference internal" href="../command/find_package.html#command:find_package" title="find_package"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">find_package()</span></code></a> in
<a class="reference internal" href="../command/find_package.html#config-mode"><span class="std std-ref">Config mode</span></a>.  By default, this module now searches for that
configuration file and, if found, returns the results without further
action.  If the upstream configuration file is not found, this module falls
back to <a class="reference internal" href="../command/find_package.html#module-mode"><span class="std std-ref">Module mode</span></a> and searches standard locations.</p>
</div>
<section id="imported-targets">
<h2>Imported Targets<a class="headerlink" href="#imported-targets" title="Link to this heading">¶</a></h2>
<p>This module provides the following <a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">Imported Targets</span></a>:</p>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.20.</span></p>
</div>
<p>Target encapsulating the usage requirements of the GoogleTest <code class="docutils literal notranslate"><span class="pre">gtest</span></code>
library, available if GoogleTest is found.  The <code class="docutils literal notranslate"><span class="pre">gtest</span></code> library provides
the core GoogleTest testing framework functionality.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">GTest::gtest_main</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.20.</span></p>
</div>
<p>Target encapsulating the usage requirements of the GoogleTest <code class="docutils literal notranslate"><span class="pre">gtest_main</span></code>
library, available if GoogleTest is found.  The <code class="docutils literal notranslate"><span class="pre">gtest_main</span></code> library
provides a <code class="docutils literal notranslate"><span class="pre">main()</span></code> function, allowing tests to be run without defining
one manually.</p>
<p>Only link to <code class="docutils literal notranslate"><span class="pre">GTest::gtest_main</span></code> if GoogleTest should supply the
<code class="docutils literal notranslate"><span class="pre">main()</span></code> function for the executable.  If the project is supplying its
own <code class="docutils literal notranslate"><span class="pre">main()</span></code> implementation, link only to <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">GTest::gmock</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.23.</span></p>
</div>
<p>Target encapsulating the usage requirements of the GoogleMock <code class="docutils literal notranslate"><span class="pre">gmock</span></code>
library, available if GoogleTest and its Mock library are found.  The
<code class="docutils literal notranslate"><span class="pre">gmock</span></code> library provides facilities for writing and using mock classes
in C++.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">GTest::gmock_main</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.23.</span></p>
</div>
<p>Target encapsulating the usage requirements of the GoogleMock <code class="docutils literal notranslate"><span class="pre">gmock_main</span></code>
library, available if GoogleTest and <code class="docutils literal notranslate"><span class="pre">gmock_main</span></code> are found.  The
<code class="docutils literal notranslate"><span class="pre">gmock_main</span></code> library provides a <code class="docutils literal notranslate"><span class="pre">main()</span></code> function, allowing GoogleMock
tests to be run without defining one manually.</p>
<p>Only link to <code class="docutils literal notranslate"><span class="pre">GTest::gmock_main</span></code> if GoogleTest should supply the
<code class="docutils literal notranslate"><span class="pre">main()</span></code> function for the executable.  If project is supplying its own
<code class="docutils literal notranslate"><span class="pre">main()</span></code> implementation, link only to <code class="docutils literal notranslate"><span class="pre">GTest::gmock</span></code>.</p>
</dd>
</dl>
</section>
<section id="result-variables">
<h2>Result Variables<a class="headerlink" href="#result-variables" title="Link to this heading">¶</a></h2>
<p>This module defines the following variables:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">GTest_FOUND</span></code></dt><dd><p>Boolean indicating whether GoogleTest is found.</p>
</dd>
</dl>
</section>
<section id="hints">
<h2>Hints<a class="headerlink" href="#hints" title="Link to this heading">¶</a></h2>
<p>This module accepts the following variables before calling
<code class="docutils literal notranslate"><span class="pre">find_package(GTest)</span></code>:</p>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">GTEST_ROOT</span></code></dt><dd><p>The root directory of the GoogleTest installation (may also be set as an
environment variable).  This variable is used only when GoogleTest is found
in <a class="reference internal" href="../command/find_package.html#module-mode"><span class="std std-ref">Module mode</span></a>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">GTEST_MSVC_SEARCH</span></code></dt><dd><p>When compiling with MSVC, this variable controls which GoogleTest build
variant to search for, based on the runtime library linkage model.  This
variable is used only when GoogleTest is found in <a class="reference internal" href="../command/find_package.html#module-mode"><span class="std std-ref">Module mode</span></a> and
accepts one of the following values:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">MD</span></code></dt><dd><p>(Default) Searches for shared library variants of GoogleTest that are
built to link against the dynamic C runtime.  These libraries are
typically compiled with the MSVC runtime flags <code class="docutils literal notranslate"><span class="pre">/MD</span></code> or <code class="docutils literal notranslate"><span class="pre">/MDd</span></code> (for
Release or Debug, respectively).</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">MT</span></code></dt><dd><p>Searches for static library variants of GoogleTest that are built to
link against the static C runtime.  These libraries are typically
compiled with the MSVC runtime flags <code class="docutils literal notranslate"><span class="pre">/MT</span></code> or <code class="docutils literal notranslate"><span class="pre">/MTd</span></code>.</p>
</dd>
</dl>
</dd>
</dl>
</section>
<section id="deprecated-items">
<h2>Deprecated Items<a class="headerlink" href="#deprecated-items" title="Link to this heading">¶</a></h2>
<section id="deprecated-variables">
<h3>Deprecated Variables<a class="headerlink" href="#deprecated-variables" title="Link to this heading">¶</a></h3>
<p>The following variables are provided for backward compatibility:</p>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">GTEST_INCLUDE_DIRS</span></code></dt><dd><div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 4.1: </span>Use the <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code> imported target instead, which exposes the
required include directories through its
<span class="target" id="index-0-prop_tgt:INTERFACE_INCLUDE_DIRECTORIES"></span><a class="reference internal" href="../prop_tgt/INTERFACE_INCLUDE_DIRECTORIES.html#prop_tgt:INTERFACE_INCLUDE_DIRECTORIES" title="INTERFACE_INCLUDE_DIRECTORIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_INCLUDE_DIRECTORIES</span></code></a> target property.</p>
</div>
<p>Result variable that provides include directories containing headers
needed to use GoogleTest.  This variable is only guaranteed to be available
when GoogleTest is found in <a class="reference internal" href="../command/find_package.html#module-mode"><span class="std std-ref">Module mode</span></a>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">GTEST_LIBRARIES</span></code></dt><dd><div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 4.1: </span>Use the <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code> imported target instead.</p>
</div>
<p>Result variable providing libraries needed to link against to use the
GoogleTest <code class="docutils literal notranslate"><span class="pre">gtest</span></code> library.  Note that projects are also responsible
for linking with an appropriate thread library in addition to the libraries
specified by this variable.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">GTEST_MAIN_LIBRARIES</span></code></dt><dd><div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 4.1: </span>Use the <code class="docutils literal notranslate"><span class="pre">GTest::gtest_main</span></code> imported target instead.</p>
</div>
<p>Result variable providing libraries needed to link against to use the
GoogleTest <code class="docutils literal notranslate"><span class="pre">gtest_main</span></code> library.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">GTEST_BOTH_LIBRARIES</span></code></dt><dd><div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 4.1: </span>Use the <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code> and <code class="docutils literal notranslate"><span class="pre">GTest::gtest_main</span></code> imported targets
instead.</p>
</div>
<p>Result variable providing both <code class="docutils literal notranslate"><span class="pre">gtest</span></code> and <code class="docutils literal notranslate"><span class="pre">gtest_main</span></code> libraries
combined.</p>
</dd>
</dl>
</section>
<section id="deprecated-imported-targets">
<h3>Deprecated Imported Targets<a class="headerlink" href="#deprecated-imported-targets" title="Link to this heading">¶</a></h3>
<p>For backward compatibility, this module also provides the following imported
targets (available since CMake 3.5):</p>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">GTest::GTest</span></code></dt><dd><div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 3.20: </span>Use the <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code> imported target instead.</p>
</div>
<p>Imported target linking the <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code> library.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">GTest::Main</span></code></dt><dd><div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 3.20: </span>Use the <code class="docutils literal notranslate"><span class="pre">GTest::gtest_main</span></code> imported target instead.</p>
</div>
<p>Imported target linking the <code class="docutils literal notranslate"><span class="pre">GTest::gtest_main</span></code> library.</p>
</dd>
</dl>
</section>
</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<section id="examples-finding-googletest">
<h3>Examples: Finding GoogleTest<a class="headerlink" href="#examples-finding-googletest" title="Link to this heading">¶</a></h3>
<p>Finding GoogleTest:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">find_package(</span><span class="nb">GoogleTest</span><span class="nf">)</span>
</pre></div>
</div>
<p>Or, finding GoogleTest and making it required (if not found, processing stops
with an error message):</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">find_package(</span><span class="nb">GoogleTest</span><span class="w"> </span><span class="no">REQUIRED</span><span class="nf">)</span>
</pre></div>
</div>
</section>
<section id="examples-using-imported-targets">
<h3>Examples: Using Imported Targets<a class="headerlink" href="#examples-using-imported-targets" title="Link to this heading">¶</a></h3>
<p>In the following example, the <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code> imported target is linked to
a project target, which enables using the core GoogleTest testing framework:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">find_package(</span><span class="nb">GTest</span><span class="w"> </span><span class="no">REQUIRED</span><span class="nf">)</span>

<span class="nf">target_link_libraries(</span><span class="nb">foo</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">GTest</span><span class="o">::</span><span class="nb">gtest</span><span class="nf">)</span>
</pre></div>
</div>
<p>In the next example, the <code class="docutils literal notranslate"><span class="pre">GTest::gtest_main</span></code> imported target is also linked
to the executable, and a test is registered.  The <code class="docutils literal notranslate"><span class="pre">GTest::gtest_main</span></code> library
provides a <code class="docutils literal notranslate"><span class="pre">main()</span></code> function, so there is no need to write one manually.
The <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code> library is still linked because the test code directly
uses things provided by <code class="docutils literal notranslate"><span class="pre">GTest::gtest</span></code>, and good practice is to link directly
to libraries used directly.</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">enable_testing()</span>

<span class="nf">find_package(</span><span class="nb">GTest</span><span class="w"> </span><span class="no">REQUIRED</span><span class="nf">)</span>

<span class="nf">add_executable(</span><span class="nb">foo</span><span class="w"> </span><span class="nb">foo.cc</span><span class="nf">)</span>
<span class="nf">target_link_libraries(</span><span class="nb">foo</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">GTest</span><span class="o">::</span><span class="nb">gtest</span><span class="w"> </span><span class="nb">GTest</span><span class="o">::</span><span class="nb">gtest_main</span><span class="nf">)</span>

<span class="nf">add_test(</span><span class="no">NAME</span><span class="w"> </span><span class="nb">AllTestsInFoo</span><span class="w"> </span><span class="no">COMMAND</span><span class="w"> </span><span class="nb">foo</span><span class="nf">)</span>
</pre></div>
</div>
</section>
</section>
<section id="deeper-integration-with-ctest">
<h2>Deeper Integration With CTest<a class="headerlink" href="#deeper-integration-with-ctest" title="Link to this heading">¶</a></h2>
<p>This module is commonly used with the <span class="target" id="index-0-module:GoogleTest"></span><a class="reference internal" href="GoogleTest.html#module:GoogleTest" title="GoogleTest"><code class="xref cmake cmake-module docutils literal notranslate"><span class="pre">GoogleTest</span></code></a> module, which
provides <span class="target" id="index-0-command:gtest_discover_tests"></span><a class="reference internal" href="GoogleTest.html#command:gtest_discover_tests" title="gtest_discover_tests"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">gtest_discover_tests()</span></code></a> and <span class="target" id="index-0-command:gtest_add_tests"></span><a class="reference internal" href="GoogleTest.html#command:gtest_add_tests" title="gtest_add_tests"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">gtest_add_tests()</span></code></a>
commands to help integrate GoogleTest infrastructure with CTest:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">find_package(</span><span class="nb">GTest</span><span class="nf">)</span>
<span class="nf">target_link_libraries(</span><span class="nb">example</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">GTest</span><span class="o">::</span><span class="nb">gtest</span><span class="w"> </span><span class="nb">GTest</span><span class="o">::</span><span class="nb">gtest_main</span><span class="nf">)</span>

<span class="nf">include(</span><span class="nb">GoogleTest</span><span class="nf">)</span>
<span class="nf">gtest_discover_tests(</span><span class="nb">example</span><span class="nf">)</span>

<span class="c"># ...</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.9: </span>Previous CMake versions defined the <span class="target" id="index-1-command:gtest_add_tests"></span><a class="reference internal" href="GoogleTest.html#command:gtest_add_tests" title="gtest_add_tests"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">gtest_add_tests()</span></code></a> command in
this module.</p>
</div>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3>Table of Contents</h3>
    <ul>
<li><a class="reference internal" href="#">FindGTest</a><ul>
<li><a class="reference internal" href="#imported-targets">Imported Targets</a></li>
<li><a class="reference internal" href="#result-variables">Result Variables</a></li>
<li><a class="reference internal" href="#hints">Hints</a></li>
<li><a class="reference internal" href="#deprecated-items">Deprecated Items</a><ul>
<li><a class="reference internal" href="#deprecated-variables">Deprecated Variables</a></li>
<li><a class="reference internal" href="#deprecated-imported-targets">Deprecated Imported Targets</a></li>
</ul>
</li>
<li><a class="reference internal" href="#examples">Examples</a><ul>
<li><a class="reference internal" href="#examples-finding-googletest">Examples: Finding GoogleTest</a></li>
<li><a class="reference internal" href="#examples-using-imported-targets">Examples: Using Imported Targets</a></li>
</ul>
</li>
<li><a class="reference internal" href="#deeper-integration-with-ctest">Deeper Integration With CTest</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="FindGSL.html"
                          title="previous chapter">FindGSL</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="FindGTK.html"
                          title="next chapter">FindGTK</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/module/FindGTest.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="FindGTK.html" title="FindGTK"
             >next</a> |</li>
        <li class="right" >
          <a href="FindGSL.html" title="FindGSL"
             >previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" >cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">FindGTest</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
    &#169; Copyright 2000-2025 Kitware, Inc. and Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
    </div>
<script type="text/javascript">
(function() {
  "use strict";
  const hide = () => document.getElementById("sidebar-check").checked = false;
  addEventListener("keydown", e => (e.key === "Escape") && hide());
  addEventListener("click", e => (e.target.tagName === "A") && hide());
  addEventListener("hashchange", hide)
})();
</script>
  </body>
</html>